/**
 * File		: Face.java
 * Subject	: Represents a cube face
 * Author	: Isaac Parenteau(locus2k@gmail.com)
 * Date		: April 16th, 2011
 * Note 	: Code was borrowed from Google and modified to fit the projects use
 * Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.	
 */
package src.project.cube;

import java.nio.ShortBuffer;
import java.util.ArrayList;

import android.util.Log;

import src.project.util.Color;
import src.project.util.Vertex;

public class Face {

	private final static String name = Face.class.getSimpleName();
	
	private ArrayList<Vertex> vertexList = new ArrayList<Vertex>();
	private Color color;
	
	/**
	 * Constructor
	 * Vertexes are counter clockwise from top left to bottom left
	 * @param topLeft top left vertex
	 * @param topRight top right vertex
	 * @param bottomRight bottom right vertex
	 * @param bottomLeft bottom left vertex
	 */
	public Face(Vertex topLeft, Vertex topRight, Vertex bottomRight, Vertex bottomLeft){
		
		addVertex(topLeft);
		addVertex(topRight);
		addVertex(bottomRight);
		addVertex(bottomLeft);
	}
	
	/**
	 * Adds a vertex to the vertex list
	 * @param vertex vertex to add
	 */
	public void addVertex(Vertex vertex){
		vertexList.add(vertex);
	}
	
	/**
	 * Sets the color of the face
	 * @param c color to set to
	 */
	public void setColor(Color c){
		int last = vertexList.size() - 1;
		
		if(last < 2)
			Log.v(name, "not enough vertices in SetColor()");
		else
		{
			Vertex vertex = vertexList.get(last);
			if(this.color == null){
				while(vertex.Color != null){
					vertexList.add(0, vertex);
					vertexList.remove(last + 1);
					vertex = vertexList.get(last);
				}
			}
			vertex.Color = c;
		}
		this.color = c;
	}
	
	/**
	 * Gets the index count
	 * @return
	 */
	public int getIndexCount(){
		return (vertexList.size() - 2) * 3;
	}
	
	/**
	 * puts the indices into the indices buffer
	 * @param buffer
	 */
	public void putIndices(ShortBuffer buffer){
		int lastIndex = vertexList.size() - 1;
		
		Vertex first = vertexList.get(0);
		Vertex last = vertexList.get(lastIndex);
		
		for(int i = 1; i < lastIndex; i++){
			Vertex current = vertexList.get(i);
			buffer.put(first.Index);
			buffer.put(current.Index);
			buffer.put(last.Index);
			first = current;
		}
	}
	
	/**
	 * gets the color of the face 
	 * @return
	 */
	public Color getColor() { return color; }
	
	/**
	 * Gets the vertex count
	 * @return
	 */
	public int getVertexCount(){
		return vertexList.size();
	}
	
	/**
	 * Gets the vertex list
	 * @return
	 */
	public ArrayList<Vertex> getVertices(){
		return vertexList;
	}
	
	/**
	 * Gets a vertex at a given location
	 * @param i location of vertex
	 * @return
	 */
	public Vertex getVertex(int i){
		return vertexList.get(i);
	}
}
